package com.student;

import java.util.ArrayList;
import java.util.Scanner;

class Student {
    private String id;
    private String name;
    private int age;
    private String address;

    public Student() {
    }

    public Student(String id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    // 补全正确的Getter和Setter方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

public class StudentManagementSystem {
    static ArrayList<Student> studentList = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            printMenu();
            int choice = getValidIntInput("请输入您的选择: ", 1, 5); // 新增输入验证

            switch (choice) {
                case 1:
                    addStudentWithDuplicateCheck(); // 新增学号重复检查
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    viewStudents();
                    break;
                case 5:
                    System.out.println("退出系统，感谢使用！");
                    return;
            }
        }
    }

    // 优化后的菜单打印方法
    private static void printMenu() {
        System.out.println("\n-----欢迎使用学生管理系统----");
        System.out.println("1. 添加学生");
        System.out.println("2. 删除学生");
        System.out.println("3. 修改学生");
        System.out.println("4. 查看所有学生");
        System.out.println("5. 退出");
    }

    // 添加学生（含学号重复检查）
    private static void addStudentWithDuplicateCheck() {
        String id;
        do {
            System.out.print("请输入学生学号: ");
            id = sc.nextLine();
            if (getIndex(id) != -1) {
                System.out.println("该学号已存在，请重新输入！");
            }
        } while (getIndex(id) != -1); // 循环直到输入唯一学号

        System.out.print("请输入学生姓名: ");
        String name = sc.nextLine();
        int age = getValidIntInput("请输入学生年龄: ", 1, 200); // 年龄范围验证
        System.out.print("请输入学生住址: ");
        String address = sc.nextLine();

        studentList.add(new Student(id, name, age, address));
        System.out.println("添加成功！");
    }

    // 查看学生（完美对齐表格）
    public static void viewStudents() {
        if (studentList.isEmpty()) {
            System.out.println("无信息，请先添加学生！");
            return;
        }

        // 表头对齐：学号(10字符)、姓名(8字符)、年龄(6字符)、住址(12字符)
        System.out.println("\n" + String.format("%-10s%-8s%-6s%-12s", "学号", "姓名", "年龄", "住址"));
        System.out.println("----------------------------------------"); // 分隔线

        for (Student s : studentList) {
            String ageStr = s.getAge() + "岁"; // 年龄带单位
            // 内容对齐：与表头宽度一致，左对齐
            System.out.println(String.format("%-10s%-8s%-6s%-12s",
                    s.getId(),
                    s.getName(),
                    ageStr,
                    s.getAddress()));
        }
    }

    // 删除学生（含学号存在检查）
    public static void deleteStudent() {
        System.out.print("请输入要删除的学生学号: ");
        String id = sc.nextLine();
        int index = getIndex(id);
        if (index == -1) {
            System.out.println("该学号不存在！");
        } else {
            studentList.remove(index);
            System.out.println("删除成功！");
        }
    }

    // 修改学生（含学号存在检查）
    public static void updateStudent() {
        System.out.print("请输入要修改的学生学号: ");
        String id = sc.nextLine();
        int index = getIndex(id);
        if (index == -1) {
            System.out.println("该学号不存在！");
            return;
        }

        System.out.print("请输入新姓名: ");
        String name = sc.nextLine();
        int age = getValidIntInput("请输入新年龄: ", 1, 200); // 年龄验证
        System.out.print("请输入新住址: ");
        String address = sc.nextLine();

        Student s = studentList.get(index);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        System.out.println("修改成功！");
    }

    // 获取学生索引（学号查找）
    public static int getIndex(String sid) {
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getId().equals(sid)) {
                return i;
            }
        }
        return -1;
    }

    // 通用整数输入验证（防止InputMismatchException）
    private static int getValidIntInput(String prompt, int min, int max) {
        while (true) {
            try {
                System.out.print(prompt);
                int num = Integer.parseInt(sc.nextLine());
                if (num >= min && num <= max) {
                    return num;
                }
                System.out.println("输入范围应为 " + min + " 到 " + max + "！");
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的整数！");
            }
        }
    }
}